package jmu.cyxwcy.librarymanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jmu.cyxwcy.librarymanagement.pojo.Classes;
import jmu.cyxwcy.librarymanagement.pojo.College;
import jmu.cyxwcy.librarymanagement.pojo.Major;
import jmu.cyxwcy.librarymanagement.mapper.MajorMapper;
import jmu.cyxwcy.librarymanagement.service.ICollegeService;
import jmu.cyxwcy.librarymanagement.service.IMajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-26
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements IMajorService {

    @Autowired
    private ICollegeService iCollegeService;

    @Override
    public List<Major> findByCollegeNamPartial(String majorNamePartial) {
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("major_name", majorNamePartial);
        return list(queryWrapper);
    }

    @Override
    public List<Major> findByCollegeName(String collegeName, String majorNamePartial) {
        Integer collegeId = iCollegeService.findIdByCollegeName(collegeName);
            QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("college_id", collegeId)
                    .likeRight("major_name", majorNamePartial);
            return list(queryWrapper);

    }

    @Override
    public Integer findIdByMajorName(String majorName) {
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("major_name", majorName);
        Major major = this.getOne(queryWrapper);
        return major != null ? major.getMajorId() : null;
    }

    @Override
    public List<Major> findByCollegeId(Integer collegeId) {
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("college_id", collegeId);
        return this.list(queryWrapper);
    }
}
